package com.cuotigou.service.impl;

import com.cuotigou.mapper.SMethodMapper;
import com.cuotigou.mapper.SUserToMethodMapper;
import com.cuotigou.pojo.SMethod;
import com.cuotigou.pojo.SUserToMethod;
import com.cuotigou.service.MethodService;
import com.cuotigou.util.*;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/8.
 */
@Service
@Transactional
public class MethodServiceImpl implements MethodService {

    private static final Logger log = LoggerFactory.getLogger(MethodServiceImpl.class);

    @Autowired
    private SMethodMapper sMethodMapper;
    @Autowired
    private SUserToMethodMapper sUserToMethodMapper;

    @Override
    public ObjectResult methodIndex(XMLBean xmlBean) {
        String userId = CommonUtils.decToken(xmlBean.getToken());

        Map<String,Object> map = new HashMap<>();
        map.put("gradeId",xmlBean.getId());
        if ("全部".equals(xmlBean.getSubject())){
            map.put("subject",null);
        }else {
            map.put("subject",xmlBean.getSubject());
        }
        if (StringUtil.isEmpty(xmlBean.getTitle())){
            map.put("title",xmlBean.getTitle());
        }else {
            map.put("title","%"+xmlBean.getTitle()+"%");
        }
        map.put("userId",userId);
        map.put("isCollection",xmlBean.getIsCollection());

        List<SMethod> sMethods = sMethodMapper.selectMethodListByUserId(map);
        //分页
        Page<SMethod> page = new Page<>();
        page.setP(xmlBean.getP()==null? 1:Integer.parseInt(xmlBean.getP()));
        List<SMethod> list = page.getPageContent(sMethods);

        for (SMethod sMethod:list){
            //是否收藏
            SUserToMethod userToMethod = new SUserToMethod();
            userToMethod.setMethodId(sMethod.getId());
            userToMethod.setUserId(Long.parseLong(userId));

            SUserToMethod userToMethod1 = sUserToMethodMapper.select(userToMethod);
            if (userToMethod1==null){
                sMethod.setIsCollection(0);
            }else {
                sMethod.setIsCollection(userToMethod1.getIsCollection());
            }
        }

        map.clear();
        map = new HashMap<>();
        map.put("list",list);
        map.put("count",page.getPageTotal());
        return ObjectResult.build("1","成功",map);
    }

    @Override
    public ObjectResult collectionAndFabulous(XMLBean xmlBean) {
        String userId = CommonUtils.decToken(xmlBean.getToken());
        SMethod sMethod = sMethodMapper.selectByPrimaryKey(Long.parseLong(xmlBean.getId()));

        Map<String,Object> map = new HashMap<>();
        try {
            SUserToMethod sUserToMethod = new SUserToMethod();
            sUserToMethod.setMethodId(Long.parseLong(xmlBean.getId()));
            sUserToMethod.setUserId(Long.parseLong(userId));
            SUserToMethod userToMethod = sUserToMethodMapper.select(sUserToMethod);

            int count = 0 ;
            sMethod.setIsCollection(sMethod.getCollection()+count);
            // 1收藏  2点赞
            if ("1".equals(xmlBean.getType())){
                //没有记录就添加，有记录则修改
                if(userToMethod==null){
                    sUserToMethod.setIsCollection(1);
                    sUserToMethod.setIsFabulous(0);
                    sUserToMethod.setAddTime(new Date());
                    sUserToMethod.setModifyTime(new Date());
                    sUserToMethodMapper.insert(sUserToMethod);
                    count = 1;
                }else {
                    if(userToMethod.getIsCollection()==0){
                        userToMethod.setIsCollection(1);
                        count = 1;
                    }else {
                        userToMethod.setIsCollection(0);
                        count = -1;
                    }
                    sUserToMethodMapper.update(userToMethod);
                }
                //修改收藏数
                sMethod.setCollection(sMethod.getCollection()+count);
            }
            if ("2".equals(xmlBean.getType())){
                //没有记录就添加，有记录则修改
                if(userToMethod==null){
                    sUserToMethod.setIsCollection(0);
                    sUserToMethod.setIsFabulous(1);
                    sUserToMethod.setAddTime(new Date());
                    sUserToMethod.setModifyTime(new Date());
                    sUserToMethodMapper.insert(sUserToMethod);
                }else {
                    if(userToMethod.getIsFabulous()==0){
                        userToMethod.setIsFabulous(1);
                        count = 1;
                    }else {
                        userToMethod.setIsFabulous(0);
                        count = -1;
                    }
                    sUserToMethodMapper.update(userToMethod);
                }
                //修改点赞数
                sMethod.setFabulous(sMethod.getFabulous()+count);
            }

            sMethodMapper.updateByPrimaryKeySelective(sMethod);

            //返回数据
            SUserToMethod userToMethod2 = sUserToMethodMapper.select(sUserToMethod);
            map.put("isCollection",userToMethod2.getIsCollection());
            map.put("isFabulous",userToMethod2.getIsFabulous());
            map.put("collection",sMethod.getCollection());
            map.put("fabulous",sMethod.getFabulous());

        }catch (RuntimeException e){
            ExceptionUtil.logError(e,log);
            ObjectResult.build("0","系统错误");
        }
        return ObjectResult.build("1","操作成功",map);
    }

    @Override
    public ObjectResult collection(XMLBean xmlBean) {
        String userId = CommonUtils.decToken(xmlBean.getToken());
        Map<String,Object> map = new HashMap<>();
        List<SMethod> sMethods = sMethodMapper.selectMethodCollection(Long.parseLong(userId));
        //分页
        Page<SMethod> page = new Page<>();
        page.setP(xmlBean.getP() == null ? 1 : Integer.parseInt(xmlBean.getP()));
        List<SMethod> list = page.getPageContent(sMethods);
        map.put("list",list);
        map.put("count",page.getPageTotal());


        return ObjectResult.build("1","成功",map);
    }

    @Override
    public SMethod methodDetails(XMLBean xmlBean) {
        String userId = CommonUtils.decToken(xmlBean.getToken());
        SMethod sMethod = sMethodMapper.selectByPrimaryKey(Long.parseLong(xmlBean.getId()));

        SUserToMethod sUserToMethod = new SUserToMethod();
        sUserToMethod.setUserId(Long.parseLong(userId));
        sUserToMethod.setMethodId(Long.parseLong(xmlBean.getId()));
        SUserToMethod userToMethod = sUserToMethodMapper.select(sUserToMethod);

        //是否收藏和点赞
        if (userToMethod==null){
            sMethod.setIsCollection(0);
            sMethod.setIsFabulous(0);
        }else {
            sMethod.setIsCollection(userToMethod.getIsCollection());
            sMethod.setIsFabulous(userToMethod.getIsFabulous());
        }
        sMethod.setTime(DateUtil.formatDateTime(sMethod.getAddTime()));
        sMethod.setContent(StringEscapeUtils.unescapeHtml4(sMethod.getContent()));

        return sMethod;
    }

    @Override
    public ObjectResult methodRecommend(XMLBean xmlBean) {
        SMethod sMethod = sMethodMapper.selectByPrimaryKey(Long.parseLong(xmlBean.getId()));

        Map<String,Object> map = new HashMap<>();
        map.put("subjectId",sMethod.getSubjectId());
        map.put("id",xmlBean.getId());
        map.put("limst",5);
        List<SMethod> sMethods = sMethodMapper.selectMethodListByUserId(map);
        return ObjectResult.build("1","成功",sMethods);
    }
}
